<template>
    <h2>显示</h2>
    科室姓名
    <input type="text" v-model="Name"/>
    科室介绍
    <input type="text" v-model="Info"/>
    创建时间
    <input type="date" v-model="Time"/>
    <input type="button" value="查询" @click="Show"/>
    <input type="text" v-model="path" />
    <input type="button" value="导出" @click="DaoChu"/>
    <input type="text" v-model="path" />
    <input type="button" value="导入" @click="DaoRu"/>
    <table border="1">
        <thead>
            <tr>
                <td>科室编号</td>
                <td>科室姓名</td>
                <td>科室介绍</td>
                <td>添加人</td>
                <td>添加时间</td>
                <td>操作</td>
            </tr>
        </thead>
        <tbody>
            <tr v-for="item in info">
                <td>{{item.D_Id}}</td>
                <td>{{item.D_Name}}</td>
                <td>{{item.D_Info}}</td>
                <td>{{item.D_AddUser}}</td>
                <td>{{moment( item.D_AddInfoDate).format("YYYY-MM-DD")}}</td>
                <td>
                    <input type="button" value="删除" @click="Shan(item.D_Id)"/>
                    <input type="button" value="修改" @click="Xiu(item.D_Id)"/>
                </td>
            </tr>
        </tbody>
    </table>  
</template>
<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import {useRoute,useRouter} from 'vue-router';
    const route=useRoute();
    const router=useRouter();
    import moment from 'moment';

    onMounted(()=>{
        Show();
    })

    const info=ref([{
         D_Id: 0,
        D_Name: "",
        D_Info: "",
        D_AddUser: "",
        D_AddInfoDate: ""
    }]);

    const Name=ref("");
    const Info=ref("");
    const Time=ref("");

    const Show=()=>{
        axios.get("https://localhost:7010/api/Dept/GetDept",{
            params:{
                Name:Name.value,
                Info:Info.value,
                Time:Time.value
            }
        })
        .then(res=>{
            console.log(res.data);
            info.value=res.data;
        })
        .catch(err=>{
            console.log(err);
        })
    }


    const Shan=(id:number)=>{
        if(confirm("确定要删除吗？")){
            axios.get("https://localhost:7010/api/Dept/DelDept",{
            params:{
                id:id
            }
        })
        .then(res=>{
            if(res.data>0){
                alert('删除成功');
                Show();
            }
            else{
                alert('删除失败');
            }
        })
        .catch(err=>{
            console.log(err);
        })
        }
    }

    const Xiu=(id:number)=>{
        router.push({
            name:"UpdateDept",
            params:{
                id:id
            }
        })
    }

    const path=ref("");

    const DaoChu=()=>{
        axios.get("https://localhost:7010/api/Dept/DaoChu",{
            params:{
                path:path.value
            }
        })
        .then(res=>{
            if(res.data>0){
                alert('导出成功');
            }
            else{
                alert('导出失败');
            }
        })
    }

    const DaoRu=()=>{
        axios.get("https://localhost:7010/api/Dept/DaoRu",{
            params:{
                path:path.value
            }
        })
        .then(res=>{
            if(res.data>0){
                console.log(res.data);
                alert('导入成功');
            }
            else{
                alert('导入失败');
            }
        })
    }

</script>
